package utils {
	import beans.ResultBean;
	import common.Common;
	
	import components.FullViewPanel;
	import components.MessageItem;
	
	import flash.events.Event;
	
	import modules.IMessagePanel;
	
	import mx.core.Application;
	import mx.rpc.AsyncToken;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.mxml.HTTPService;
	
	[Event(name="responsecorrectresultevent", type="mx.events.FlexEvent")]

	public class WhHTTPService extends HTTPService {
		
		//------------------------------------------------------------
        //
        // variables
        //
        //------------------------------------------------------------
		
		private var _timerId:uint;
		
		//------------------------------------------------------------
        //
        // overriden methods
        //
        //------------------------------------------------------------
		
		/**
		 * append a random number parameter when sending reqeust
		 */
		override public function send(parameters:Object=null):AsyncToken {
            if (!parameters) {
            	parameters = {};
            }
            parameters.random = Math.random();
            
//            _timerId = setTimeout(Application.application.waitStart, 3000);
            
            return super.send(parameters);
		}
		
		//------------------------------------------------------------
        //
        // methods
        //
        //------------------------------------------------------------
		
		private function getMessagePanel():FullViewPanel {
			var imp:IMessagePanel = IMessagePanel(Application.application.infoPanel.moduleLoader.child);
			if (!imp) {
				return null;
			}
			
			var mp:FullViewPanel = imp.getMessagePanel();
			if (!mp) {
				return null;
			}
			
			return mp;
		}
		
		private function onFault(e:FaultEvent):void {
//			clearTimeout(_timerId);
			Application.application.waitEnd();
			
			var mp:FullViewPanel = getMessagePanel();
			if (!mp) {
				return;
			}
			
			mp.setMessageItem(MessageItem.RESULT_ERROR);
		}
		
		private function onResult(e:ResultEvent):void {
//			clearTimeout(_timerId);
			Application.application.waitEnd();
			
			var mp:FullViewPanel = getMessagePanel();
			if (!mp) {
				return;
			}
			
			var result:ResultBean = new ResultBean(XML(e.result.common));
			
			if (result.errorCode == "0") {
				dispatchEvent(new Event(Common.RESPONSE_CORRECT_EVENT, true));
			} else if (result.errorCode == "datasubmit") {
				mp.setMessageItem(MessageItem.RESULT_OK);
				dispatchEvent(new Event(Common.RESPONSE_CORRECT_EVENT, true));
				return;
			} else if (result.errorCode == "roomclick") {
				// if the result is belong to room click request,
				// don't display result
				return;
			} else {
				mp.setMessageItem(MessageItem.RESULT_WARNING, MessageMap.getMessage(result.errorCode));
			}
		}
		
		//------------------------------------------------------------
        //
        // constructor
        //
        //------------------------------------------------------------
		
		public function WhHTTPService(rootURL:String=null, destination:String=null) {
			super(rootURL, destination);
			
			addEventListener(FaultEvent.FAULT, onFault);
			addEventListener(ResultEvent.RESULT, onResult);
		}
		
	}
}